package com.course.servlet;

import com.course.entity.Student;
import com.course.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/guanliyuan_student_manage")
public class GuanliyuanStudentManageServlet extends HttpServlet {
    private StudentService studentService = new StudentService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        System.out.println("GuanliyuanStudentManageServlet: Processing GET request");
        try {
            String studentId = request.getParameter("studentId");
            String studentName = request.getParameter("studentName");
            String className = request.getParameter("className");

            List<Student> students;
            if (studentId != null && !studentId.trim().isEmpty() ||
                studentName != null && !studentName.trim().isEmpty() ||
                className != null && !className.trim().isEmpty()) {
                // If any search parameter is provided, perform a filtered search
                students = studentService.searchStudents(studentId, studentName, className);
            } else {
                // Otherwise, get all students
                students = studentService.getAllStudents();
            }

            request.setAttribute("students", students);
            request.getRequestDispatcher("/WEB-INF/jsp/guanliyuan_student_manage.jsp").forward(request, response);
        } catch (Exception e) {
            //System.err.println("GuanliyuanStudentManageServlet: Error processing request");
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error retrieving student data");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String action = request.getParameter("action");

        if ("delete".equals(action)) {
            String studentIdStr = request.getParameter("id");
            try {
                int id = Integer.parseInt(studentIdStr);
                boolean success = studentService.deleteStudent(id);

                if (success) {
                    request.getSession().setAttribute("successMessage", "学生账号删除成功！");
                } else {
                    request.getSession().setAttribute("errorMessage", "学生账号删除失败，请稍后重试。");
                }
            } catch (NumberFormatException e) {
                request.getSession().setAttribute("errorMessage", "无效的学生ID。");
            } catch (Exception e) {
                request.getSession().setAttribute("errorMessage", "删除学生账号时发生错误：" + e.getMessage());
                e.printStackTrace();
            }
        }
        response.sendRedirect(request.getContextPath() + "/guanliyuan_student_manage");
    }
} 